package com.lxmes.hr.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcOrgManage;
import com.lxmes.hr.domain.NcPieceworkMoneySummary;
import com.lxmes.hr.mapper.NcOrgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcRePunAssessMapper;
import com.lxmes.hr.domain.NcRePunAssess;
import com.lxmes.hr.service.INcRePunAssessService;

import javax.validation.Validator;

/**
 * 奖惩考核管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-24
 */
@Service
public class NcRePunAssessServiceImpl implements INcRePunAssessService
{
    @Autowired
    private NcRePunAssessMapper ncRePunAssessMapper;
    @Autowired
    private NcOrgManageMapper ncOrgManageMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询奖惩考核管理
     *
     * @param id 奖惩考核管理主键
     * @return 奖惩考核管理
     */
    @Override
    public NcRePunAssess selectNcRePunAssessById(Long id)
    {
        return ncRePunAssessMapper.selectNcRePunAssessById(id);
    }

    /**
     * 查询奖惩考核管理列表
     *
     * @param ncRePunAssess 奖惩考核管理
     * @return 奖惩考核管理
     */
    @Override
    public List<NcRePunAssess> selectNcRePunAssessList(NcRePunAssess ncRePunAssess)
    {
        return ncRePunAssessMapper.selectNcRePunAssessList(ncRePunAssess);
    }

    /**
     * 新增奖惩考核管理
     *
     * @param ncRePunAssess 奖惩考核管理
     * @return 结果
     */
    @Override
    public int insertNcRePunAssess(NcRePunAssess ncRePunAssess)
    {
        ncRePunAssess.setCreateBy(SecurityUtils.getUsername());
        ncRePunAssess.setCreateTime(DateUtils.getNowDate());
        List<NcOrgManage> ncOrgManages = ncOrgManageMapper.selectNcOrgManageList(null);
        if(!StringUtils.isNull(ncOrgManages)){
            ncRePunAssess.setFactoryCode(ncOrgManages.get(0).getJtdm());
            ncRePunAssess.setFactoryName(ncOrgManages.get(0).getJtmc());
        }
        BigDecimal count = ncRePunAssess.getRpNum1()
                .add(ncRePunAssess.getRpNum2())
                .add(ncRePunAssess.getRpNum3())
                .add(ncRePunAssess.getRpNum4())
                .add(ncRePunAssess.getRpNum5())
                .add(ncRePunAssess.getRpNum6())
                .add(ncRePunAssess.getRpNum7())
                .add(ncRePunAssess.getRpNum8())
                .add(ncRePunAssess.getRpNum9())
                .add(ncRePunAssess.getRpNum10())
                .add(ncRePunAssess.getRpNum11())
                .add(ncRePunAssess.getRpNum12())
                .add(ncRePunAssess.getRpNum13())
                .add(ncRePunAssess.getRpNum14())
                .add(ncRePunAssess.getRpNum15())
                .add(ncRePunAssess.getRpNum16())
                .add(ncRePunAssess.getRpNum17())
                .add(ncRePunAssess.getRpNum18())
                .add(ncRePunAssess.getRpNum19())
                .add(ncRePunAssess.getRpNum20())
                .add(ncRePunAssess.getRpNum21())
                .add(ncRePunAssess.getRpNum22())
                .add(ncRePunAssess.getRpNum23())
                .add(ncRePunAssess.getRpNum24())
                .add(ncRePunAssess.getRpNum25())
                .add(ncRePunAssess.getRpNum26())
                .add(ncRePunAssess.getRpNum27())
                .add(ncRePunAssess.getRpNum28())
                .add(ncRePunAssess.getRpNum29())
                .add(ncRePunAssess.getRpNum30())
                .add(ncRePunAssess.getRpNum31());
        ncRePunAssess.setRePunPrice(count);
        return ncRePunAssessMapper.insertNcRePunAssess(ncRePunAssess);
    }

    /**
     * 修改奖惩考核管理
     *
     * @param ncRePunAssess 奖惩考核管理
     * @return 结果
     */
    @Override
    public int updateNcRePunAssess(NcRePunAssess ncRePunAssess)
    {
        ncRePunAssess.setUpdateTime(DateUtils.getNowDate());
        ncRePunAssess.setUpdateBy(SecurityUtils.getUsername());
        BigDecimal count = ncRePunAssess.getRpNum1().add(ncRePunAssess.getRpNum2())
                .add(ncRePunAssess.getRpNum3())
                .add(ncRePunAssess.getRpNum4())
                .add(ncRePunAssess.getRpNum5())
                .add(ncRePunAssess.getRpNum6())
                .add(ncRePunAssess.getRpNum7())
                .add(ncRePunAssess.getRpNum8())
                .add(ncRePunAssess.getRpNum9())
                .add(ncRePunAssess.getRpNum10())
                .add(ncRePunAssess.getRpNum11())
                .add(ncRePunAssess.getRpNum12())
                .add(ncRePunAssess.getRpNum13())
                .add(ncRePunAssess.getRpNum14())
                .add(ncRePunAssess.getRpNum15())
                .add(ncRePunAssess.getRpNum16())
                .add(ncRePunAssess.getRpNum17())
                .add(ncRePunAssess.getRpNum18())
                .add(ncRePunAssess.getRpNum19())
                .add(ncRePunAssess.getRpNum20())
                .add(ncRePunAssess.getRpNum21())
                .add(ncRePunAssess.getRpNum22())
                .add(ncRePunAssess.getRpNum23())
                .add(ncRePunAssess.getRpNum24())
                .add(ncRePunAssess.getRpNum25())
                .add(ncRePunAssess.getRpNum26())
                .add(ncRePunAssess.getRpNum27())
                .add(ncRePunAssess.getRpNum28())
                .add(ncRePunAssess.getRpNum29())
                .add(ncRePunAssess.getRpNum30())
                .add(ncRePunAssess.getRpNum31());
        ncRePunAssess.setRePunPrice(count);
        return ncRePunAssessMapper.updateNcRePunAssess(ncRePunAssess);
    }

    /**
     * 批量删除奖惩考核管理
     *
     * @param ids 需要删除的奖惩考核管理主键
     * @return 结果
     */
    @Override
    public int deleteNcRePunAssessByIds(Long[] ids)
    {
        return ncRePunAssessMapper.deleteNcRePunAssessByIds(ids);
    }

    /**
     * 删除奖惩考核管理信息
     *
     * @param id 奖惩考核管理主键
     * @return 结果
     */
    @Override
    public int deleteNcRePunAssessById(Long id)
    {
        return ncRePunAssessMapper.deleteNcRePunAssessById(id);
    }

    @Override
    public String imporIteminfo(List<NcRePunAssess> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcRePunAssess org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcRePunAssess(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工工号 " + org.getJobNo() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getJobNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
